/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-5-12 下午3:41
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.recycleviewdemo.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.dqhl.recycleviewdemo.R;
import com.example.dqhl.recycleviewdemo.util.CircleDrawable;
import com.example.dqhl.recycleviewdemo.util.UtilMoreText;
import com.example.dqhl.recycleviewdemo.view.CircleImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * description:   伸缩的Textview  自定义Drawable
 * autour:guanluocang
 * date:17/5/15 11:27
 */
public class MoreTextViewActivity extends AppCompatActivity {

    private TextView tv1;
    private TextView tv2;
    private int open;
    private String msg = "在Android开发中，有许多信息展示需要通过TextView来展现，" +
            "如果只是普通的信息展现，使用TextView setText(CharSequence str)设置即可，" +
            "但是当在TextView里的这段内容需要截取某一部分字段，可以被点击以及响应响应的操作，" +
            "这时候就需要用到SpannableString了，下面通过一段简单的代码实现部分文字被点击响应，及富文本表情的实现";

    private CircleImageView iv_bitmap;

    private EditText et_input;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_text_view);

        (et_input) = (EditText) findViewById(R.id.et_input);

        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv2.setTextSize(20);
        new UtilMoreText(tv2, msg, 100);
        tv1.setText(getClickableSpan(0));
        tv1.setMovementMethod(LinkMovementMethod.getInstance());

        //自定义Drawable
        (iv_bitmap) = (CircleImageView) findViewById(R.id.iv_bitmap);
        // https://pic2.zhimg.com/v2-55d0c21338bdbb6255add38dcb3af8b9_b.jpg

        Glide.with(MoreTextViewActivity.this).load("https://pic2.zhimg.com/v2-55d0c21338bdbb6255add38dcb3af8b9_b.jpg").into(iv_bitmap);
        new Thread(new Runnable() {
            @Override
            public void run() {
//                Bitmap bimap = getImageBitmap("https://pic2.zhimg.com/v2-55d0c21338bdbb6255add38dcb3af8b9_b.jpg");
//        Bitmap bimap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_five);
//                iv_bitmap.setImageDrawable(new CircleDrawable(bimap));
//                iv_bitmap.setImageBitmap(bimap);
                Glide.with(MoreTextViewActivity.this).load("https://pic2.zhimg.com/v2-55d0c21338bdbb6255add38dcb3af8b9_b.jpg").into(iv_bitmap);
            }
        });


        StringBuffer stringBuffer = new StringBuffer(et_input.getText().toString().trim());
        stringBuffer.insert(et_input.getText().length() - 1, "哈哈");
    }

    private SpannableString getClickableSpan(int flag) {
        View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv1.setText(getClickableSpan(open));
                Toast.makeText(MoreTextViewActivity.this, "000", Toast.LENGTH_SHORT).show();
            }
        };
        SpannableString spanableInfo = new SpannableString(
                "This is a test, Click ...Me");
        int start = spanableInfo.length() - 5;
        int end = spanableInfo.length();
        switch (flag) {
            case 0:
                spanableInfo = new SpannableString(
                        "This is a test, Click ...Me");
                start = spanableInfo.length() - 5;
                end = spanableInfo.length();
                spanableInfo.setSpan(new Clickable(l), start, end,
                        Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                open = 1;
                break;
            case 1:
                spanableInfo = new SpannableString(
                        "在Android开发中，有许多信息展示需要通过TextView来展现，如果只是普通的信息展现，" +
                                "使用TextView setText(CharSequence str)设置即可，但是当在TextView里的这段内容需要截取某一部分字段，" +
                                "可以被点击以及响应响应的操作，这时候就需要用到SpannableString了，" +
                                "下面通过一段简单的代码实现部分文字被点击响应，及富文本表情的实现 ...收起");
                start = spanableInfo.length() - 5;
                end = spanableInfo.length();
                spanableInfo.setSpan(new Clickable(l), start, end,
                        Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                open = 0;
                break;
        }

        return spanableInfo;
    }

    /**
     * 内部类，用于截获点击富文本后的事件
     *
     * @author pro
     */
    class Clickable extends ClickableSpan implements View.OnClickListener {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener l) {
            mListener = l;
        }

        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ds.linkColor);
            ds.setUnderlineText(false);    //去除超链接的下划线
        }
    }

    /**
     * 将富文本转成CharSequence
     *
     * @param commonStr 普通内容
     * @param bqId      表情图片
     * @return
     */
    public CharSequence transferBiaoQing(String commonStr, int bqId) {
        return Html.fromHtml(commonStr + "<img src=\"" + bqId + "\">", imageGetter, null);
    }

    /**
     * 获取本地图片资源
     */
    private Html.ImageGetter imageGetter = new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            int id = Integer.parseInt(source);
            // 根据id从资源文件中获取图片对象
            Drawable d = MoreTextViewActivity.this.getApplicationContext().getResources().getDrawable(id);
            // 以此作为标志位，方便外部取出对应的资源id
            d.setState(new int[]{id});
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            return d;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("GG", keyCode + "");
        return super.onKeyDown(keyCode, event);
    }

    public Bitmap getImageBitmap(String url) {
        URL imgUrl = null;
        Bitmap bitmap = null;
        try {
            imgUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imgUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}
